package day13;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: 王丹
 * @create: 2021-07-06 09:35
 *
 **/

//Condition.await()过程中被打断
public class Demo4 {
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    public static class T extends Thread {
        public void run() {
            lock.lock();
            try {
                condition.await();
            } catch (InterruptedException e) {
                System.out.println("中断标志" + this.isInterrupted());
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    /**
     * 运行结果分析：调用condition.await()之后，线程进入阻塞中，调用t1.interrupt()，给t1线程发送中断
     * 信号，await()方法内部会检测到线程中断信号，然后触发 InterruptedException异常，线程中断标志被
     * 清除。从输出结果中可以看出，线程t1中断标志的变换过程：false->true->false
     */
    public static void main(String[] args) throws InterruptedException {
        T t1 = new T();
        t1.setName("t1");
        t1.start();
        TimeUnit.SECONDS.sleep(2);
        System.out.println("1、t1中断标志位：" + t1.isInterrupted());
        t1.interrupt();
        System.out.println("2、t2中断标志位：" + t1.isInterrupted());

    }
}
